package com.mozz.finally_work.dao;

import com.mozz.finally_work.pojo.Blog;
import com.mozz.finally_work.pojo.Tag;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface BlogDao {

    /*添加博客*/
    @Insert({"<script>",
            "INSERT INTO blog ",
            "<if test='cover==null'>(id,title,content,public_time,user_id) values(#{id},#{title},#{content},#{publicTime},#{user.id})</if>",
            "<if test='cover!=null'>(id,title,content,public_time,user_id,cover) values(#{id},#{title},#{content},#{publicTime},#{user.id},#{cover})</if>",
            "</script>"})
    Integer addBlog(Blog blog);

    /*更新博客*/
    @Update({"<script>",
            "Update blog set ",
            "title=#{title},content=#{content},public_time=#{publicTime} <if test='cover!=null'>,cover=#{cover}</if> where id=#{id}",
            "</script>"})
    Integer updateBlog(Blog blog);

    /*删除博客*/
    @Delete("DELETE FROM blog where id=#{id}")
    Integer deleteBlog(String blogId);


    /*获取所有的博客时间排序*/
    @Select("SELECT * FROM blog order by public_time desc")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "public_time", property = "publicTime"),
            @Result(property = "user", column = "user_id", one = @One(select = "com.mozz.finally_work.dao.UserDao.findUserById", fetchType = FetchType.EAGER)),
            @Result(property = "tags", column = "id", many = @Many(select = "com.mozz.finally_work.dao.TagDao.queryTagByBlogId", fetchType = FetchType.LAZY))
    })
    List<Blog> queryAllBlog();

    /*获取所有的博客，分页，时间排序*/
    @Select("SELECT * FROM blog order by public_time desc limit #{offset},#{pageSize}")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "public_time", property = "publicTime"),
            @Result(property = "user", column = "user_id", one = @One(select = "com.mozz.finally_work.dao.UserDao.findUserById", fetchType = FetchType.EAGER)),
            @Result(property = "tags", column = "id", many = @Many(select = "com.mozz.finally_work.dao.TagDao.queryTagByBlogId", fetchType = FetchType.LAZY))
    })
    List<Blog> queryAllBlogPage(@Param("offset") int offset,@Param("pageSize") int pageSize);

    /*查询博客条数*/
    @Select("SELECT count(*) FROM blog ")
    Integer queryBlogCount();


    /*添加博客标签*/
    @Insert("INSERT INTO blog_tag(id,blog_id,tag_id) values(#{id},#{blogId},#{tagId})")
    Integer addBlogTag(@Param("id") String id, @Param("blogId") String blogId, @Param("tagId") String tagId);

    /*添加博客分类*/
    @Insert("INSERT INTO blog_category(id, category_id, blog_id) values(#{id},#{categoryId},#{blogId})")
    Integer addBlogCategory(@Param("id") String id, @Param("blogId") String blogId, @Param("categoryId") String tagId);


    /*根据id查询文章*/
    @Select("SELECT * FROm blog where id=#{id}")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "public_time", property = "publicTime"),
            @Result(property = "user", column = "user_id", one = @One(select = "com.mozz.finally_work.dao.UserDao.findUserById", fetchType = FetchType.EAGER)),
            @Result(property = "tags", column = "id", many = @Many(select = "com.mozz.finally_work.dao.TagDao.queryTagByBlogId", fetchType = FetchType.LAZY))
    })
    Blog queryBlogById(String id);

    /*根据标签id查询文章*/
    @Select("SELECT * FROM blog join blog_tag on blog.id=blog_tag.blog_id where blog_tag.tag_id=#{tagId}")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "public_time", property = "publicTime"),
            @Result(property = "user", column = "user_id", one = @One(select = "com.mozz.finally_work.dao.UserDao.findUserById", fetchType = FetchType.EAGER)),
            @Result(property = "tags", column = "id", many = @Many(select = "com.mozz.finally_work.dao.TagDao.queryTagByBlogId", fetchType = FetchType.LAZY))
    })
    List<Blog> queryBlogsByTagId(String tagId);

    /*根据分类id查询所有文章*/
    @Select("SELECT * FROM blog join blog_category on blog.id=blog_category.blog_id where blog_category.category_id=#{categoryId}")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "public_time", property = "publicTime"),
            @Result(property = "user", column = "user_id", one = @One(select = "com.mozz.finally_work.dao.UserDao.findUserById", fetchType = FetchType.EAGER)),
            @Result(property = "tags", column = "id", many = @Many(select = "com.mozz.finally_work.dao.TagDao.queryTagByBlogId", fetchType = FetchType.LAZY))
    })
    List<Blog> queryBlogsByCategoryId(String categoryId);

    /*根据用户id获取文章条数*/
    @Select("SELECT count(*) as blogCount from blog where user_id=#{userId}")
    Integer countBlog(String userId);

    /*根据用户id查询所有文章*/
    @Select("SELECT *  from blog where user_id=#{userId} order by public_time desc")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "public_time", property = "publicTime"),
            @Result(property = "user", column = "user_id", one = @One(select = "com.mozz.finally_work.dao.UserDao.findUserById", fetchType = FetchType.EAGER)),
            @Result(property = "tags", column = "id", many = @Many(select = "com.mozz.finally_work.dao.TagDao.queryTagByBlogId", fetchType = FetchType.LAZY))
    })
    List<Blog> queryAllBlogByUserId(String userId);

    /*根据用户id获取所有收藏的文章*/
    @Select("SELECT * from blog join collection on collection.blog_id=blog.id join user on user.id=collection.user_id where user.id=#{userId}")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "public_time", property = "publicTime"),
            @Result(property = "user", column = "user_id", one = @One(select = "com.mozz.finally_work.dao.UserDao.findUserById", fetchType = FetchType.EAGER)),
            @Result(property = "tags", column = "id", many = @Many(select = "com.mozz.finally_work.dao.TagDao.queryTagByBlogId", fetchType = FetchType.LAZY))
    })
    List<Blog> queryCollectionBlogs(String userId);

}
